package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.UserPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：lizimo
 * @date ：Created in 2020/1/2
 * @description ：
 * @version: 1.0
 */


@Slf4j
@RestController
@RequestMapping("/permission")
public class UserPermissionController {

    @Reference
    private UserPermissionService userPermissionService;

    /**
     * 新增权限
     *
     * @param permission
     * @return
     */

    @PostMapping("add")
    public Result add(@RequestBody Permission permission) {
        log.info("[权限-新增]data:{}", permission);

        userPermissionService.add(permission);

        return new Result(true, MessageConst.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */

    @RequestMapping("delete")
    public Result delete(@RequestBody Integer id) {
        log.info("[权限-删除]data:{}", id);
        //rpc调用
        userPermissionService.delete(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);

    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        log.info("[权限-编辑]data:{}", permission);

        //rpc查询数据
        userPermissionService.edit(permission);
        return new Result(true, MessageConst.EDIT_PERMISSION_SUCCESS);

    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.info("[权限-根据Id查询]data:{}",id);
        Permission permission = userPermissionService.findById(id);
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permission);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        log.info("[权限-查询所有]data:{}");
        List<Permission> permissions = userPermissionService.findAll();

        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissions);
    }

    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[权限-分页查询]",queryPageBean);
        PageResult pageResult = userPermissionService.pageQuery(queryPageBean);
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
    }

}
